package com.mooc.apigateway.service;

import com.google.common.base.Joiner;
import com.mooc.apigateway.common.ListResponse;
import com.mooc.apigateway.common.UserContext;
import com.mooc.apigateway.common.page.PageData;
import com.mooc.apigateway.common.page.PageParams;
import com.mooc.apigateway.dao.CommentDao;
import com.mooc.apigateway.dao.CommunityDao;
import com.mooc.apigateway.dao.HouseDao;
import com.mooc.apigateway.dao.UserDao;
import com.mooc.apigateway.model.*;
import com.mooc.apigateway.utils.BeanHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HouseService {

    @Autowired
    private CommunityDao communityDao;

    @Autowired
    private HouseDao houseDao;

    @Autowired
    private FileService fileService;

    @Autowired
    private UserDao userDao;



    @Autowired
    private CommentDao commentDao;

    public List<Community> getAllCommunitys() {
          return communityDao.getAllCommunity();
    }




    public void addHouse(House house, User user) {
        house.setUserId(user.getId());
        if (CollectionUtils.isNotEmpty(house.getHouseFiles())) {
            String images = Joiner.on(",").join(fileService.getImgPaths(house.getHouseFiles()));
            house.setImages(images);
        }
        if (CollectionUtils.isNotEmpty(house.getFloorPlanFiles())) {
            String images = Joiner.on(",").join(fileService.getImgPaths(house.getFloorPlanFiles()));
            house.setFloorPlan(images);
        }
        BeanHelper.onInsert(house);
        houseDao.add(house);
    }


    public PageData<House> queryHouse(House query, PageParams pageParams) {
       /* List<House> houses = Lists.newArrayList();
        if (!Strings.isNullOrEmpty(query.getName())) {
            Community community = new Community();
            community.setName(query.getName());
            List<Community> communities = houseDao.selectCommunity(community);
            if (!communities.isEmpty()) {
                query.setCommunityId(communities.get(0).getId());
            }
        }
        houses = queryAndSetImg(query,pageParams);//查询结构后添加图片服务器地址前缀
        Long count = houseDao.selectPageCount(query);*/
        HouseQueryReq hq = new HouseQueryReq();
        hq.setQuery(query);
        hq.setLimit(pageParams.getLimit());
        hq.setOffset(pageParams.getOffset());
        ListResponse<House> re = houseDao.queryHouse(hq);
        List<House> houses = re.getList();
        Long count = re.getCount();
        return PageData.buildPage(houses, count, pageParams.getPageSize(), pageParams.getPageNum());
  }

    public House getDetailById(Long houseId) {
        House house = houseDao.queryHouseDetail(houseId);
        return house;
    }

    public  List<House> queryRecommdHouses(Integer size){
        return houseDao.queryRecommdHouses(size);
    }



/*    public List<House> queryAndSetImg(House query, PageParams pageParams) {
        List<House> houses = houseDao.selectPageHouse(query, pageParams);
        houses.forEach(h -> {
            h.setFirstImg(imgPrefix + h.getFirstImg());
            h.setImageList(h.getImageList().stream().map(img -> imgPrefix + img).collect(Collectors.toList()));
            h.setFloorPlanList(h.getFloorPlanList().stream().map(img -> imgPrefix + img).collect(Collectors.toList()));

        });
        return houses;
    }*/

    //得到房屋销售代理人
    public User querySaleHouseAgent(Long houseId) {

        //1.先根据houseId 查询到userId
        Long userId = houseDao.queryUserIdByHouseId(houseId);
        //2.根据userId得到代理人具体信息
        User user = userDao.getAgentById(userId);
        return user;
    }


    public void bookMark(Integer id) {
        User user = UserContext.getUser();
        HouseUser houseUser = new HouseUser();
        houseUser.setUserId(user.getId());
        houseUser.setHouseId(id.longValue());
        houseUser.setType(2);//收藏
        houseDao.bookMark(houseUser);

    }

    public void unbookmark(Integer id) {
        User user = UserContext.getUser();
        HouseUser houseUser = new HouseUser();
        houseUser.setUserId(user.getId());
        houseUser.setHouseId(id.longValue());
        houseUser.setType(2);//收藏
        houseDao.unbookMark(houseUser);
    }


    public void addUserMsg(UserMsg userMsg) {
        User user = UserContext.getUser();
        userMsg.setUserId(user.getId());
        BeanHelper.onInsert(userMsg);
        commentDao.insertUserMsg(userMsg);
        userDao.sendUserMail(userMsg);
       /* User agent = agencyService.getAgentDeail(userMsg.getAgentId());
        mailService.sendMail("来自用户"+userMsg.getEmail()+"的留言", userMsg.getMsg(), agent.getEmail());
*/
    }


}
